#include "AutoPtrTests.h"
#include "../TestFramework/TestAutoPtr.h"

namespace
{
    struct Test
    {
        Test()
            : First(5)
        {
        }

        int First;
    };

    TEST_CASE(TestEmptyAutoPtr)
    {
        TestFramework::TestAutoPtr<Test> ptr;
        CHECK(!ptr.Get());
    }

    TEST_CASE(TestAutoPtrCreation)
    {
        TestFramework::TestAutoPtr<Test> ptr(new Test());
        CHECK(ptr.Get());
    }

    TEST_CASE(TestAutoPtrCopying1)
    {
        TestFramework::TestAutoPtr<Test> ptr1(new Test());
        TestFramework::TestAutoPtr<Test> ptr2(ptr1);
        CHECK(!ptr1.Get() && ptr2.Get());
    }

    TEST_CASE(TestAutoPtrCopying2)
    {
        TestFramework::TestAutoPtr<Test> ptr1(new Test());
        TestFramework::TestAutoPtr<Test> ptr2 = ptr1;
        CHECK(!ptr1.Get() && ptr2.Get());
    }

    TEST_CASE(TestAutoPtrNullReseting)
    {
        TestFramework::TestAutoPtr<Test> ptr(new Test());
        ptr.Reset();
        CHECK(!ptr.Get());
    }

    TEST_CASE(TestAutoPtrReseting)
    {
        TestFramework::TestAutoPtr<Test> ptr1(new Test());
        TestFramework::TestAutoPtr<Test> ptr2;
        ptr2.Reset(ptr1.Release());
        CHECK(ptr2.Get() && !ptr1.Get());
    }

    TEST_CASE(TestAutoPtrDataAccessing)
    {
        TestFramework::TestAutoPtr<Test> ptr(new Test());
        CHECK((ptr.Get()->First == 5) && ((*ptr).First == 5) && (ptr->First == 5));
    }

    START_TEST_SUITE(AutoPtrTest, "AutoPtr test")
    {
        REGISTER_TEST(TestEmptyAutoPtr);
        REGISTER_TEST(TestAutoPtrCreation);
        REGISTER_TEST(TestAutoPtrCopying1);
        REGISTER_TEST(TestAutoPtrCopying2);
        REGISTER_TEST(TestAutoPtrNullReseting);
        REGISTER_TEST(TestAutoPtrReseting);
        REGISTER_TEST(TestAutoPtrDataAccessing);
    }
    FINISH_TEST_SUITE
}

namespace Tests
{
    TestFramework::UnitTestPtr CreateAutoPtrTester()
    {
        CREATE_SUITE(AutoPtrTest)
    }
}
